<template>
  <el-card class="category" style="margin-bottom: 20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="c1Id">
          <el-option
            v-for="c1 in category1List"
            :label="c1.name"
            :value="c1.id"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="c2Id">
          <el-option
            v-for="c2 in category2List"
            :label="c2.name"
            :value="c2.id"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="c3Id">
          <el-option
            v-for="c3 in category3List"
            :label="c3.name"
            :value="c3.id"
            :key="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import { mapState, mapActions } from "vuex";
export default {
  name: "CategorySelector",
  methods: {
    ...mapActions("categoryList", [
      "getCategory1List",
      "getCategory2List",
      "getCategory3List",
      "setCategory3Id",
    ]),
  },
  // 当页面挂载完毕以后就执行代码
  mounted() {
    this.getCategory1List();
  },
  computed: {
    //吧state和getter映射在computed中
    ...mapState("categoryList", [
      "category1Id",
      "category2Id",
      "category3Id",
      "category1List",
      "category2List",
      "category3List",
    ]),
    c1Id: {
      get() {
        return this.category1Id;
      },
      set(newV) {
        this.getCategory2List(newV);
      },
    },
    c2Id: {
      get() {
        return this.category2Id;
      },
      set(newV) {
        this.getCategory3List(newV);
      },
    },
    c3Id: {
      get() {
        return this.category3Id;
      },
      set(newV) {
        this.setCategory3Id(newV);
      },
    },
  },
};
</script>
<style>
.category .el-card__body .el-form-item {
  margin-bottom: 0;
}
</style>
